<?php
    session_start();
    require_once('../ns_html.class.php');
    
    if (isset($_SESSION['username']))
    {
        $thisUser = $html->nsDatabase->UserByUsername($_SESSION['username']);
    }
    
    
    if (isset($_POST['uploadAvatar']))
    {
        if ($_FILES["avatarFile"]["error"] > 0)
        {
            echo "Error: " . $_FILES["avatarFile"]["error"] . "<br />";
            header("Location: my_profile.php");
        }
        else
        {
            echo "Upload: " . $_FILES["avatarFile"]["name"] . "<br />";
            echo "Type: " . $_FILES["avatarFile"]["type"] . "<br />";
            echo "Size: " . ($_FILES["avatarFile"]["size"] / 1024) . " Kb<br />";
            echo "Stored in: " . $_FILES["avatarFile"]["tmp_name"];

            //$thisUser
            //$_FILES["avatarFile"]
            $path_parts = pathinfo($_FILES["avatarFile"]["name"]);
            $ext = $path_parts['extension'];
            $newFilename = $thisUser->UserID() . "-" .  $thisUser->Username() . "-avatar" . "." . $ext;

            move_uploaded_file($_FILES['avatarFile']['tmp_name'], "../../content/avatar/" . $newFilename);
            $html->nsDatabase->updateUser($thisUser->UserID(), "AvatarImage", "../../content/avatar/" . $newFilename);

            echo "<br />";
            echo $path_parts . "<br />";
            echo $ext . "<br />";
            echo $newFilename . "<br />";
            header("Location: my_profile.php");
        }
    }

    if (isset($_POST['updateUserProfile']))
    {
        
        
    }
?>
